﻿using UnityEngine;
using System.Collections;

namespace Battle
{
    public class BattleHeroItemGudieCtrl : MonoBehaviour
    {
        BattlePanel battlePanel;

        BattleHeroItemGuide focusItem;


        bool IsUltimating = false;

        bool IsOtherGuideShowing
        {
            get
            {
                if (battlePanel.guidePanel.GetComponent<ComboGuidePanel>().IsShowing)
                    return true;
                return false;
            }
        }

        public void EnterUltimate()
        {
            IsUltimating = true;
        }

        public void LeaveUltimate()
        {
            IsUltimating = false;
        }
        void Start()
        {
            battlePanel = BattleManager.Instance.battlePanel;
            if (battlePanel.viewCtrl.isTipGuideNeed)
            {
                for (int i = 0; i < battlePanel.heroItems.Count; ++i)
                {
                    battlePanel.heroItems[i].gameObject.AddMissingComponent<BattleHeroItemGuide>();
                }
            }
        }

        bool isNeedRevert = false;
        bool isNeedRevert0 = false;

        void Update()
        {
            if (battlePanel.transferPanel.IsShowing())
            {
                if (focusItem != null)
                {
                    focusItem.HideTip();
                    isNeedRevert0 = true;
                }
                return;
            }
            else
            {
                if (isNeedRevert0)
                {
                    if (focusItem.currentGuideType == BattleHeroItemGuide.GuideType.NormalAttack)
                    {
                        focusItem.ShowNormalAttackTip();
                    }
                    else if (focusItem.currentGuideType == BattleHeroItemGuide.GuideType.Ultimate)
                    {
                        focusItem.ShowUltimateAttackTip();
                    }
                    else
                    {
                        focusItem.HideTip();
                    }
                    isNeedRevert0 = false;
                }
            }


            if (IsOtherGuideShowing)
            {
                if (focusItem != null)
                {
                    focusItem.HideTip();
                    isNeedRevert = true;
                }
                return;
            }
            else
            {
                if (isNeedRevert)
                {
                    if (focusItem.currentGuideType == BattleHeroItemGuide.GuideType.NormalAttack)
                    {
                        focusItem.ShowNormalAttackTip();
                    }
                    else if (focusItem.currentGuideType == BattleHeroItemGuide.GuideType.Ultimate)
                    {
                        focusItem.ShowUltimateAttackTip();
                    }
                    else
                    {
                        focusItem.HideTip();
                    }
                    isNeedRevert = false;
                }
            }

            if (IsUltimating)
            {
                return;
            }

            if (focusItem != null)
            {
                bool isValid = UpdateGuideItem(focusItem);
                if (!isValid)
                {
                    focusItem = null;
                }
            }
            else
            {
                focusItem = GetFocusItem();
            }
        }

        BattleHeroItemGuide GetFocusItem()
        {
            for(int i = 0; i < battlePanel.heroItems.Count; ++i)
            {
                var item = battlePanel.heroItems[i];
                if (item.owner.IsUltimateSkillCanCast4UI() || item.owner.IsFirstSkillCanCast4UI() || item.owner.IsSecondSkillCanCast4UI())
                {
                    return item.GetComponent<BattleHeroItemGuide>();
                }
            }
            return null;
        }

        bool UpdateGuideItem(BattleHeroItemGuide item)
        {
            if (item.currentGuideType == BattleHeroItemGuide.GuideType.None)
            {
                if (item.heroItem.owner.IsUltimateSkillCanCast4UI())
                {
                    item.currentGuideType = BattleHeroItemGuide.GuideType.Ultimate;
                    return false;
                }
                else
                {
                    if(item.heroItem.owner.IsFirstSkillCanCast4UI() || item.heroItem.owner.IsSecondSkillCanCast4UI())
                    {
                        item.currentGuideType = BattleHeroItemGuide.GuideType.NormalAttack;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (item.currentGuideType == BattleHeroItemGuide.GuideType.NormalAttack)
            {
                if (item.heroItem.owner.IsUltimateSkillCanCast4UI())
                {
                    item.currentGuideType = BattleHeroItemGuide.GuideType.Ultimate;
                    return false;
                }
                else
                {
                    if (!item.heroItem.owner.IsFirstSkillCanCast4UI()  && !item.heroItem.owner.IsSecondSkillCanCast4UI())
                    {
                        item.currentGuideType = BattleHeroItemGuide.GuideType.None;
                    }
                }
            }
            else if (item.currentGuideType == BattleHeroItemGuide.GuideType.Ultimate)
            {
                if (!item.heroItem.owner.IsUltimateSkillCanCast4UI())
                {
                    if (item.heroItem.owner.IsFirstSkillCanCast4UI() || item.heroItem.owner.IsSecondSkillCanCast4UI())
                    {
                        item.currentGuideType = BattleHeroItemGuide.GuideType.NormalAttack;
                    }
                    else
                    {
                        item.currentGuideType = BattleHeroItemGuide.GuideType.None;
                    }
                }
            }
            return true;
        }

    }
}
